package b1.mobile.blackberry.util;

import net.rim.device.api.system.Bitmap;

/**
 * 
 * @author YangShen
 *
 */
public class ImageUtil {
	public static Bitmap resizeBitmap(Bitmap image, int width, int height) {
		int imageWidth = image.getWidth();
		int imageHeight = image.getHeight();

		int rgb[] = new int[imageWidth * imageHeight];

		image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);

		int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);

		Bitmap temp2 = new Bitmap(width, height);

		temp2.setARGB(rgb2, 0, width, 0, 0, width, height);

		return temp2;
	}

	private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2) {
		int out[] = new int[x2 * y2];
		for (int yy = 0; yy < y2; yy++) {
			int dy = yy * y / y2;
			for (int xx = 0; xx < x2; xx++) {
				int dx = xx * x / x2;
				out[(x2 * yy) + xx] = ini[(x * dy) + dx];
			}
		}
		return out;
	}

}
